<? // dml - insert/update col(s) via ajax

//-require_once( '../ajax/dd.php' );
$dd = unserialize(file_get_contents('../ajax/dd.ser'));

$request = $_GET['request'];
$args = $_GET['args'];
$rows = Array();

function colVal( $rel, $col, $val ) {
GLOBAL $dd;
	if( !$rel ) return $val;
	$notnull = @$dd[$rel]['cols'][$col]['required'] || @$dd[$rel]['cols'][$col]['notnull'];
	if( strlen($val)==0 ) {
		if( !$notnull )						return null;
		if( @$dd[$rel]['cols'][$col]['datatype'] == 'number' )	return 0;
	}
	return $val;
}

// go through each pre/idx
// todo: parents first
$ct = 0;
$inserting = false;
$deleting  = false;
foreach( array_keys($args) as $pre ) {

	// no dd - get from dd
	if( !@$args[$pre]['dd'] && @$dd[$pre] ) $args[$pre]['dd'] = $pre;

	// to tbl, pkcol
	if( !@$args[$pre]['tbl'] && @$dd[$args[$pre]['dd']] ) $args[$pre]['tbl'] = $dd[$args[$pre]['dd']]['tbl'];
	if( !@$args[$pre]['pkcol'] && @$dd[$args[$pre]['dd']] ) $args[$pre]['pkcol'] = $dd[$args[$pre]['dd']]['pkcol'];
	$rel = @$args[$pre]['dd'];

	// if no tbl, err
	if( !@$args[$pre]['tbl'] ) ajax_exit( 'failure', "pre $pre does not have tbl" );

	// update level == row - will send back row results of dml
	$rows[$pre] = Array();

	// for each row, calc key
	foreach( array_keys($args[$pre]['rows']) as $idx ) {

		// rows
		$rows[$pre][$idx] = Array();

		// set columns
		foreach( array_keys( $args[$pre]['rows'][$idx]['cols']) as $col ) {

			// rows - pass back what was given
			$rows[$pre][$idx][$col] = $args[$pre]['rows'][$idx]['cols'][$col]['value'];

			// column name
			if( !@$args[$pre]['rows'][$idx]['cols'][$col]['col'] ) $args[$pre]['rows'][$idx]['cols'][$col]['col'] = $col;
			// quote value
			$args[$pre]['rows'][$idx]['cols'][$col]['value'] =
				colVal( $rel, $args[$pre]['rows'][$idx]['cols'][$col]['col'], $args[$pre]['rows'][$idx]['cols'][$col]['value'] );
		}

		// calculate key
		if( !@$args[$pre]['rows'][$idx]['pkval'] && @$args[$pre]['rows'][$idx]['cols'][$args[$pre]['pkcol']] ) 
			$args[$pre]['rows'][$idx]['pkval'] = $args[$pre]['rows'][$idx]['cols'][$args[$pre]['pkcol']];
		if( !@$args[$pre]['rows'][$idx]['key'] && @$args[$pre]['rows'][$idx]['pkval'] ) 
			$args[$pre]['rows'][$idx]['key'] = "{$args[$pre]['pkcol']} = ".(int)($args[$pre]['rows'][$idx]['pkval']);
			//-$args[$pre]['rows'][$idx]['key'] = Array( "{$args[$pre]['pkcol']} = ?", $args[$pre]['rows'][$idx]['pkval'] );

		// delete
		if( @$args[$pre]['rows'][$idx]['dml']=='delete' || @$args[$pre]['rows'][$idx]['dlx'] ) {
			$dml = 'delete';
			$deleting = true;
			if( !@$args[$pre]['rows'][$idx]['key'] ) ajax_exit( 'failure', "$pre.$idx - cannnot delete without key" );
			$query = "delete from {$args[$pre]['tbl']} where {$args[$pre]['pkcol']} = ?";
			$queryParams = Array( $args[$pre]['rows'][$idx]['pkval'] );
			// set row to null as if select had happened
			$rows[$pre][$idx] = null;
		}
		// update
		else if( @$args[$pre]['rows'][$idx]['key'] ) {
			$dml = 'update';
			$query = "update {$args[$pre]['tbl']} set ";
			$queryParams = Array();
			foreach( array_keys( $args[$pre]['rows'][$idx]['cols']) as $col ) {
				if( $col == @$args[$pre]['pkcol'] ) continue;
				if( @$dd[$pre]['cols'][$col]['updateable']===false ) continue;
				if( $rel && @$dd[$rel]['cols'][$col]['dml'] == 'none' ) continue;
				//-$query .= "$col = {$args[$pre]['rows'][$idx]['cols'][$col]['value']}, ";
				$query .= "$col = ?,";
				$queryParams[] = $args[$pre]['rows'][$idx]['cols'][$col]['value'];
			}
			$query .= "update_usr_id={$_SESSION['usr_id']}, update_ts=current_timestamp where " . $args[$pre]['rows'][$idx]['key'];
			//-$query .= "update_usr_id=?, update_ts=current_timestamp ";
			//-$queryParams[] = $_SESSION['usr_id'];
			//-$query .= " where " . $args[$pre]['rows'][$idx]['key'][0];
			//-for( $i=1; $i<count($args[$pre]['rows'][$idx]['key']); $i++ ) $queryParams[] = $args[$pre]['rows'][$idx]['key'][$i];
		}
		// insert
		else {
			$dml = 'insert';
			$inserting = true;
			$query = "insert into {$args[$pre]['tbl']} ( ";
			$values = '';
			$queryParams = Array();
			foreach( array_keys( $args[$pre]['rows'][$idx]['cols']) as $col ) {
				if( $col == @$args[$pre]['pkcol'] ) continue;
				if( @$dd[$pre]['cols'][$col]['updateable']===false ) continue;
				if( $rel && @$dd[$rel]['cols'][$col]['dml'] == 'none' ) continue;
				$query .= "$col, ";
				//-$values .= "{$args[$pre]['rows'][$idx]['cols'][$col]['value']}, ";
				$values .= "?, ";
				$queryParams[] = $args[$pre]['rows'][$idx]['cols'][$col]['value'];
			}
			//-$query .= "insert_usr_id ) values( $values {$_SESSION['usr_id']} )";
			// unknown: why following two lines do not work
			$query .= "insert_usr_id ) values( $values ? )";
			$queryParams[] = $_SESSION['usr_id'];
		}

		//-ajax_exit( 'failure', "query: $query, params=".implode(',',$queryParams) );
		$ct = db_dml( $query, $queryParams );
		if( $dml == 'insert' ) {
			$args[$pre]['rows'][$idx]['pkval'] = db_insert_id();
			$args[$pre]['rows'][$idx]['key'] = "{$args[$pre]['pkcol']} = ".(int)($args[$pre]['rows'][$idx]['pkval']);
			//-$args[$pre]['rows'][$idx]['key'] = Array( "{$args[$pre]['pkcol']} = ?", $args[$pre]['rows'][$idx]['pkval'] );
		}
	}
}

// fullfill request

// col: keep what we have
if( $request == 'col' && (!$deleting && !$deleting) ) {
	$request = 'col';
	// do nothing
}
// rows: re-query row
elseif( $request == 'row' || ( $request=='col' && ($deleting || $inserting) )
|| ( $request=='row-form' && !$inserting ) || ( $request=='row-content' && !$inserting )) {
	$request = 'row';
	// for each pre.idx pull in row
	foreach( array_keys($args) as $pre ) {
		$tbl_v = (@$dd[$rel]['tbl_v']) ? $dd[$rel]['tbl_v'] : $dd[$rel]['tbl'];
		foreach( array_keys($args[$pre]['rows']) as $idx ) {
			$query = "select * from $tbl_v where {$args[$pre]['rows'][$idx]['key']}";
			//-$rows[$pre]['query'] = $query;
			$rows[$pre][$idx] = ($dml=='delete') ? null : db_get_rcd( $query );
		}
	}
}
// form: re-query rcds
elseif( $request == 'form' || ( $request=='row-form' && $inserting ) ) {
	$request = 'form';
	// for each pre pull in rcds
	foreach( array_keys($args) as $pre ) {
		$tbl_v = (@$dd[$rel]['tbl_v']) ? $dd[$rel]['tbl_v'] : $dd[$rel]['tbl'];
		$key = @$args[$pre]['key'];
		$query = "select * from $tbl_v ".(($key)?" where $key":'');
		$args[$pre] = db_get_rcds($query);
	}
}
// content: do not need rows.pre
elseif( $request == 'content' || ( $request=='row-content' && $inserting ) ) {
	$request = 'content';
	foreach( array_keys($rows) as $pre ) $rows[$pre] = null;
}
// success: do not need row
elseif( $request == 'success' || ( $request=='row-content' && $inserting ) ) {
	$request = 'success';
	$rows = null;
}
// unknown request
else {
	ajax_exit( 'failure', "unknown request: $request, inserting=$inserting, deleting=$deleting" );
}

ajax_exit( 'success', "$ct records affected: $query", Array( 'request'=>$request, 'rows'=>$rows ) );

?>
